Passed
Push — master ( 4fc8e9...0ebefa )
by Kevin Van
01:32 queued 12s
created

MatchTeaser.tsx ➔ getData   D

Complexity

Conditions 12

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 4.8
c 0
b 0
f 0
cc 12

How to fix   Complexity   

Complexity

Complex classes like MatchTeaser.tsx ➔ getData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import axios from "axios"
2
import classNames from "classnames"
3
import { graphql, useStaticQuery } from "gatsby"
4
import Moment from "moment-timezone"
5
import "moment/locale/nl-be"
6
import React, { Fragment, FunctionComponent, useEffect, useState } from "react"
7
import LazyLoad from "react-lazyload"
8
9
import { mapPsdStatus } from "../scripts/helper"
10
import "./MatchTeaser.scss"
11
import MiniRanking from "./MiniRanking"
12
13
export const MatchTeaserDetail: FunctionComponent<MatchTeaserDetailProps> = ({
14
  match,
15
  includeRankings = false,
16
}: MatchTeaserDetailProps) => {
17
  Moment.locale(`nl-BE`)
18
  const d = Moment.tz(match.date, `Europe/Brussels`)
19
  const matchPlayed =
20
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
21
    false
22
23
  return (
24
    <article className="match__teaser">
25
      <header>
26
        <h3>{match.teamName.replace(`Voetbal : `, ``)}</h3>
27
        <div>
28
          {match.status !== 0 && (
29
            <Fragment>
30
              <time
31
                className="match__teaser__datetime match__teaser__datetime--date"
32
                dateTime={d.format(`YYYY-MM-DD - H:mm`)}
33
              >
34
                {d.format(`dddd DD MMMM - H:mm`)}
35
              </time>
36
              <span className="match__teaser__datetime match__teaser__datetime--status">
37
                {mapPsdStatus(match.status)}
38
              </span>
39
            </Fragment>
40
          )}
41
          {(match.status === 0 || match.status === null) && (
42
            <Fragment>
43
              <time className="match__teaser__datetime match__teaser__datetime--date" dateTime={d.format(`YYYY-MM-DD`)}>
44
                {d.format(`dddd DD MMMM`)}
45
              </time>
46
              <time className="match__teaser__datetime match__teaser__datetime--time" dateTime={d.format(`H:mm`)}>
47
                {d.format(`H:mm`)}
48
              </time>
49
            </Fragment>
50
          )}
51
        </div>
52
      </header>
53
      <main>
54
        <div
55
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
56
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
57
          })}
58
        >
59
          <LazyLoad debounce={false}>
60
            <img
61
              src={match.homeClub?.logo}
62
              alt={match.homeClub?.abbreviation}
63
              className="match__teaser__logo match__teaser__logo--home"
64
            />
65
          </LazyLoad>
66
          {match.homeClub?.abbreviation || match.homeClub?.name}
67
        </div>
68
69
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
70
        {matchPlayed && (
71
          <div className="match__teaser__vs match__teaser__vs--score">
72
            <div className="match__teaser__vs--score--home">{match.goalsHomeTeam}</div>
73
            <div className="match__teaser__vs--score--away">{match.goalsAwayTeam}</div>
74
          </div>
75
        )}
76
77
        <div
78
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
79
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
80
          })}
81
        >
82
          <LazyLoad debounce={false}>
83
            <img
84
              src={match.awayClub?.logo}
85
              alt={match.awayClub?.abbreviation}
86
              className="match__teaser__logo match__teaser__logo--away"
87
            />
88
          </LazyLoad>
89
          {match.awayClub?.abbreviation || match.awayClub?.name}
90
        </div>
91
      </main>
92
      {includeRankings && match.competitionType === `Competitie` && (
93
        <MiniRanking
94
          teamId={match.homeTeamId || match.awayTeamId}
95
          homeTeam={match.homeClub?.name}
96
          awayTeam={match.awayClub?.name}
97
        />
98
      )}
99
    </article>
100
  )
101
}
102
103
export const MatchTeaser: FunctionComponent<MatchTeaserProps> = ({
104
  teamId,
105
  action,
106
  includeRankings = false,
107
}: MatchTeaserProps) => {
108
  if (action !== `prev` && action !== `next`) {
109
    throw new Error(`Invalid action provided`)
110
  }
111
112
  const [data, setData] = useState<Match[]>([])
113
114
  const {
115
    site: {
116
      siteMetadata: { kcvvPsdApi },
117
    },
118
  }: MatchesQueryData = useStaticQuery(graphql`
119
    {
120
      site {
121
        siteMetadata {
122
          kcvvPsdApi
123
        }
124
      }
125
    }
126
  `)
127
128
  useEffect(() => {
129
    async function getData() {
130
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
131
        params: { include: teamId },
132
      })
133
      setData(response.data)
134
    }
135
    getData()
136
  }, [])
137
138
  if (data.length > 0) {
139
    return <MatchTeaserDetail match={data[0]} includeRankings={includeRankings} />
140
  } else {
141
    return <div className="match__teaser__no_match">Geen wedstrijd gevonden</div>
142
  }
143
}
144
145
export const MatchTeasers: FunctionComponent<MatchTeasersProps> = ({
146
  teamId,
147
  includeRankings = false,
148
}: MatchTeasersProps) => (
149
  <div className="match__teasers">
150
    <MatchTeaser teamId={teamId} action="prev" includeRankings={includeRankings} />
151
    <MatchTeaser teamId={teamId} action="next" includeRankings={includeRankings} />
152
  </div>
153
)
154